Using STR_TO_DATE() and DATE_FORMAT() in MySQL
MySQL provides two important functions for working with date strings: STR_TO_DATE() for converting strings into date values, and DATE_FORMAT() for converting date values into formatted strings.
Parses a string based on a specified format and returns a DATE, DATETIME, or TIME value.
Useful when importing data where dates are stored as strings.
Format specifiers (e.g., %d, %m, %Y) must match the input string.
This converts the string '21-11-2025' into a proper DATE value: 2025-11-21.
Formats a date value into a custom string.
Useful for displaying dates in user-friendly formats.
Requires format specifiers similar to STR_TO_DATE().
This outputs: '21/11/2025'.
STR_TO_DATE() converts a string into a date value.
DATE_FORMAT() converts a date into a formatted string.
STR_TO_DATE() is used when reading/parsing dates; DATE_FORMAT() is used when displaying dates.
Both rely on the same set of format specifiers.
In summary: Use STR_TO_DATE() to interpret a date string into a real MySQL date, and DATE_FORMAT() to output a stored date in a human-readable format.